package io.github.wanggit.antrpc.console.web;

import io.github.wanggit.antrpc.console.service.ConfigService;
import io.github.wanggit.antrpc.console.web.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("config")
public class ConfigController {

    @Autowired private ConfigService configService;

    /**
     * 把原空间的所有key，都复制到另一个空间下
     *
     * @param oldEnvName old env
     * @param newEnvName new env
     * @param oldSpaceName old env
     * @param newSpaceName new env
     * @return result
     */
    @PostMapping("copyToNewSpace")
    public Result copyToNewSpace(
            @RequestParam("oldEnvName") String oldEnvName,
            @RequestParam("newEnvName") String newEnvName,
            @RequestParam("oldSpaceName") String oldSpaceName,
            @RequestParam("newSpaceName") String newSpaceName) {
        configService.copyToNewSpace(oldEnvName, oldSpaceName, newEnvName, newSpaceName);
        return Result.ok();
    }

    /**
     * 保存key value
     *
     * @param envName env name
     * @param spaceName space name
     * @param key key
     * @param value value
     * @param grayReleases host:port, host:port, ...
     * @return result
     */
    @PostMapping("saveKey")
    public Result saveOrUpdateKey(
            @RequestParam("envName") String envName,
            @RequestParam("spaceName") String spaceName,
            @RequestParam("key") String key,
            @RequestParam("value") String value,
            @RequestParam(value = "grayReleases", required = false) String grayReleases) {
        return configService.saveOrUpdateKey(envName, spaceName, key, value, grayReleases);
    }

    /**
     * 删除key
     *
     * @param envName env name
     * @param spaceName space name
     * @param key key
     * @return result
     */
    @PostMapping("deleteKey")
    public Result deleteKey(
            @RequestParam("envName") String envName,
            @RequestParam("spaceName") String spaceName,
            @RequestParam("key") String key) {
        return configService.deleteKey(envName, spaceName, key);
    }

    /**
     * 保存space
     *
     * @param envName env name
     * @param spaceName space name
     * @return result
     */
    @PostMapping("saveSpace")
    public Result saveOrUpdateSpace(
            @RequestParam("envName") String envName, @RequestParam("spaceName") String spaceName) {
        return configService.saveOrUpdateSpace(envName, spaceName);
    }

    @PostMapping("deleteSpace")
    public Result deleteSpace(
            @RequestParam("envName") String envName, @RequestParam("spaceName") String spaceName) {
        return configService.deleteSpace(envName, spaceName);
    }

    @PostMapping("saveEnv")
    public Result saveOrUpdateEnv(@RequestParam("envName") String envName) {
        return configService.saveEnv(envName);
    }

    @PostMapping("deleteEnv")
    public Result deleteEnv(@RequestParam("envName") String envName) {
        return configService.deleteEnv(envName);
    }
}
